<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户信息录入</title>
    <link th:href="@{/css/bootstrap.min.css}"  rel="stylesheet">
    <link th:href="@{/css/bootstrap-responsive.min.css}" rel="stylesheet">
    <link th:href="@{/css/site.css}" rel="stylesheet">
    <script th:src="@{/js/jquery.min.js}" ></script>
    <script th:src="@{/js/bootstrap.min.js}"></script>
    <script src="https://cdn.bootcss.com/jquery.serializeJSON/2.9.0/jquery.serializejson.min.js"></script>

</head>
<body>

<div class="span9">
    <h3 th:if="${user!=null}" th:text="用户信息修改">
        用户信息录入
    </h3>
    <form id="edit-profile" class="form-horizontal">
        <input type="hidden" name="_method" value="put" th:if="${user!=null}"/>
        <input type="hidden" name="id" th:if="${user!=null}" th:value="${user.id}">
        <fieldset>
            <legend>Your Profile</legend>
            <div class="control-group">
                <label class="control-label" for="username">Name</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="username" name="username" value="" th:value="${user!=null}?${user.username}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="phone">Phone</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="phone" name="phone" value="" th:value="${user!=null}?${user.phone}" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="age">age</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="age" name="age" value="" th:value="${user!=null}?${user.age}" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="email">Email</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="email" name="email" value="" th:value="${user!=null}?${user.email}" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="remark">Biography</label>
                <div class="controls">
                    <textarea class="input-xlarge" id="remark" name="remark" rows="4" th:text="${user!=null}?${user.remark}"></textarea>
                </div>
            </div>
            <div class="form-actions">
                <button type="button" id="btnSubmit" class="btn btn-primary" th:text="${user!=null}?'修改':'添加'">Save</button> <button class="btn">Cancel</button>
            </div>
        </fieldset>
    </form>
</div>
</div>

<script th:inline="javascript">
    //序列化表单字段为json对象
    // $.fn.serializeFormToJson = function(){
    //     var arr = $(this).serializeArray();//form表单数据 name：value
    //     var param = {};
    //     $.each(arr,function(i,obj){ //将form表单数据封装成json对象
    //         param[obj.name] = obj.value;
    //     })
    //     return param;
    // }

    $("#btnSubmit").click(function () {

        // var data = $("#edit-profile").serialize();
        var data = $('#edit-profile').serializeJSON();
        var id = [[${user}]];
        console.log(id);
        var postType = "";
        if (id != null){
            postType = "PUT";
        }else{
            postType = "POST";
        }
        //
        // if ($("#btnSubmit").val()=="修改"){
        //
        // }else{
        //     postType = "POST";
        // }


        //var formData = $("#edit-profile").serializeFormToJson();
        // var data = $("#tf").serializeFormToJson();
        //console.log(data);
        // console.log('are you ok?')
        $.ajax({
            url: '/user/post',
            type: postType,
            data: JSON.stringify(data),//参数值
            contentType:"application/json; charset=utf-8",//请求参数的在格式
            //请求成功时处理
            success: function (data) {
                alert(data.data);
                window.location.href="/user/userlist";
                //console.log(data);
            }
        });
    });



</script>
</body>
</html>